/**
 * askfordialog.h is part of %PROJECTNAME%.
 *
 * %PROJECTNAME% is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * %PROJECTNAME% is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with %PROJECTNAME%.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef ASKFORDIALOG_H
#define ASKFORDIALOG_H

#include "../../utils/printable.h"
#include <iostream>

template< class T >
class AskForConsoleDlg : public Printable
{
public:
    AskForConsoleDlg( const std::string & a_msg );
    virtual ~AskForConsoleDlg() {}

    T AskFor();

private:

    // Fields.
    // -------

    std::string m_msg;

    // Methods.
    // --------

    virtual std::string Print() const;

    // Protect default constructor.
    AskForConsoleDlg();

    // Protect default copy constructor.
    AskForConsoleDlg( const AskForConsoleDlg & a_other );

    // Protect default operator =.
    AskForConsoleDlg & operator = ( const AskForConsoleDlg & a_other );
};

///////////////////////////////////////////////////////////////
template< class T >
AskForConsoleDlg< T >::AskForConsoleDlg( const std::string & a_msg ) :
    Printable(),
    m_msg( a_msg )
{
}

///////////////////////////////////////////////////////////////
template< class T >
std::string AskForConsoleDlg< T >::Print() const
{
    return m_msg;
}

///////////////////////////////////////////////////////////////
template< class T >
T AskForConsoleDlg< T >::AskFor()
{
    T ret;

    std::cout << Print();

    bool ok = false;
    do
    {
        std::cin >> ret;
        ok = std::cin.good();
        if( !ok )
            std::cout << Print();
    }
    while( !ok );

    std::cin.clear();

    return ret;
}

#endif // ASKFORDIALOG_H
